﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace DroidLord.Util
{
    class PortUseKiller
    {
        private static string ExecCmd(string cmd)
        {
            Process MyProcess = new Process();
            MyProcess.StartInfo.FileName = "cmd.exe";
            MyProcess.StartInfo.UseShellExecute = false;
            MyProcess.StartInfo.RedirectStandardInput = true;
            MyProcess.StartInfo.RedirectStandardOutput = true;
            MyProcess.StartInfo.RedirectStandardError = true;
            MyProcess.StartInfo.CreateNoWindow = true;
            MyProcess.Start();
            MyProcess.StandardInput.WriteLine(cmd);
            MyProcess.StandardInput.WriteLine("exit");
            String StrInfo = MyProcess.StandardOutput.ReadToEnd();
            return StrInfo;
        }
        public static List<String> getPortUseProcessList(int port)
        {
            String cmd = "netstat -ano|findstr \"" + port + "\"";
            String info = ExecCmd(cmd);
            List<String> procesList = new List<string>();
            String[] infos = info.Split(new char[] { '\r', '\n' });
            if (null != infos)
            {
                foreach (String line in infos)
                {
                    if ((line.Trim().StartsWith("TCP") || line.Trim().StartsWith("UDP")) && (line.Contains("LISTENING") || line.Contains("ESTABLISHED") || line.Contains("TIME_WAIT")))
                    {
                        String rline = null;
                        int index = -1;
                        int index1 = line.LastIndexOf("LISTENING");
                        int index2 = line.LastIndexOf("ESTABLISHED");
                        int index3 = line.LastIndexOf("TIME_WAIT");
                        if (index1 > 0)
                        {
                            rline = line.Substring(index1 + "LISTENING".Length);
                        }
                        else if (index2 > 0)
                        {
                            rline = line.Substring(index2 + "ESTABLISHED".Length);
                        }
                        else if (index3 > 0)
                        {
                            line.Substring(index3 + "TIME_WAIT".Length);
                        }
                        else
                        {
                            continue;
                        }
                        if (rline != null)
                        {
                            //System.Windows.Forms.MessageBox.Show("line:"+line+" rline==>"+rline);
                            try
                            {
                                int pid = Convert.ToInt32(rline.Trim());
                                if (pid > 0)
                                {
                                    String cmd2 = "tasklist|findstr \"" + pid + "\"";
                                    String info2 = ExecCmd(cmd2);
                                    String[] info2s = info2.Split(new char[] { '\r', '\n' });
                                    if (info2s != null)
                                    {
                                        foreach (String temp in info2s)
                                        {
                                            if (temp.Contains("" + pid) && temp.Contains(".exe"))
                                            {
                                                int index01 = temp.IndexOf(".exe");
                                                String rmsg = temp.Substring(0, index01);
                                                //System.Windows.Forms.MessageBox.Show("process:"+rmsg);
                                                procesList.Add(rmsg + ".exe");
                                            }
                                        }
                                    }
                                }
                            }
                            catch (Exception eeeeee)
                            {

                            }
                        }
                    }
                }
            }
            return procesList;
        }


        public static void killOtherAdb(Boolean isKill)
        {
            int count = 0;
            if (isKill)
            {

                // System.Windows.Forms.MessageBox.Show("kill other");
                //if (ProcessManager.IsProcessStarted("360MobileLink"))
                //{
                //    String cmd001 = "taskkill /IM  360MobileLink.exe /F /T";
                //    ExecCmd(cmd001);
                //}
                //if (ProcessManager.IsProcessStarted("shuame_helper"))
                //{

                //    String cmd00 = "taskkill /IM  shuame_helper.exe /F /T";
                //    ExecCmd(cmd00);
                //    //System.Windows.Forms.MessageBox.Show(cmd00);
                //}
                //if (ProcessManager.IsProcessStarted("db_adb"))
                //{
                //    String cmd00 = "taskkill /IM  db_adb.exe /F /T";
                //    ExecCmd(cmd00);
                //}

                //if (ProcessManager.IsProcessStarted("PhoneService"))
                //{
                //    String cmd0 = "taskkill /IM PhoneService.exe /F /T";
                //    ExecCmd(cmd0);
                //}
                //if (ProcessManager.IsProcessStarted("tadb"))
                //{
                //    String cmd = "taskkill /IM tadb.exe /F";
                //    ExecCmd(cmd);
                //}
                //if (ProcessManager.IsProcessStarted("wandoujia_daemon"))
                //{
                //    String cmd1 = "taskkill /IM wandoujia_daemon.exe /F /T";
                //    ExecCmd(cmd1);
                //}
                //if (ProcessManager.IsProcessStarted("360shuaji"))
                //{
                //    String cmd2 = "taskkill /IM 360shuaji.exe /F /T";
                //    ExecCmd(cmd2);
                //}
                //if (ProcessManager.IsProcessStarted("kxeconn"))
                //{
                //    String cmd3 = "taskkill /IM kxeconn.exe /F /T";
                //    ExecCmd(cmd3);
                //}
                //if (ProcessManager.IsProcessStarted("AndroidInterface"))
                //{
                //    String cmd4 = "taskkill /IM AndroidInterface.exe /F /T";
                //    ExecCmd(cmd4);
                //}
                //if (ProcessManager.IsProcessStarted("sjk_daemon"))
                //{
                //    String cmd5 = "taskkill /IM sjk_daemon.exe /F /T";
                //    ExecCmd(cmd5);
                //}
                //if (ProcessManager.IsProcessStarted("AndroidServer"))
                //{
                //    String cmd5 = "taskkill /IM AndroidServer.exe /F /T";
                //    ExecCmd(cmd5);
                //}
                //count++;
                if (true)
                {
                    List<String> processList = PortUseKiller.getPortUseProcessList(5037);
                    //System.Windows.Forms.MessageBox.Show("len:"+processList.Count);
                    foreach (String pname in processList)
                    {
                        //System.Windows.Forms.MessageBox.Show("pname:"+pname);
                        if (pname.Trim().StartsWith("droidadb")/*|| pname.Trim().StartsWith("adb.exe")*/)
                        {
                            continue;
                        }
                        String cmdall = "taskkill /IM " + pname + " /F /T";
                        ExecCmd(cmdall);
                    }
                }
            }
        }
    }
}
